<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class ChannelScanQr extends \app\BaseModel
{
    protected $name = "massage_channel_scan_code_record";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getQrRegister($open_id, $arr = [])
    {
        $qr_find = $this->where(["open_id" => $open_id, "user_id" => 0])->where("type", "<>", 0)->order("id desc")->find();
        if(!empty($qr_find)) {
            $this->dataUpdate(["id" => $qr_find["id"]], ["is_new" => 1]);
            $arr["is_qr"] = 1;
            $arr["source_type"] = $qr_find["type"];
            $arr["admin_id"] = !empty($qr_find["admin_id"]) ? $qr_find["admin_id"] : $this->getQrAdminId($qr_find);
        }
        return $arr;
    }
    public function getQrAdminId($qr_find)
    {
        $list = [["type" => 1, "table" => "massage_channel_qr", "title" => "admin_id"], ["type" => 2, "table" => "massage_distribution_list", "title" => "admin_id"], ["type" => 4, "table" => "shequshop_school_admin", "title" => "id"], ["type" => 5, "table" => "shequshop_school_admin", "title" => "id"], ["type" => 6, "table" => "shequshop_school_admin", "title" => "id"], ["type" => 7, "table" => "massage_service_coach_list", "title" => "admin_id"], ["type" => 8, "table" => "massage_salesman_list", "title" => "admin_id"], ["type" => 9, "table" => "massage_channel_list", "title" => "admin_id"], ["type" => 10, "table" => "massage_distribution_list", "title" => "admin_id"], ["type" => 11, "table" => "shequshop_school_admin", "title" => "id"], ["type" => 12, "table" => "shequshop_school_admin", "title" => "id"]];
        $admin_id = 0;
        foreach ($list as $v) {
            if($v["type"] == $qr_find["type"]) {
                if($v["type"] == 2) {
                    $admin_id = \think\facade\Db::name($v["table"])->where(["user_id" => $qr_find["qr_id"], "status" => 2])->value($v["title"]);
                } else {
                    $admin_id = \think\facade\Db::name($v["table"])->where(["id" => $qr_find["qr_id"]])->value($v["title"]);
                }
                $admin_id = !empty($admin_id) ? $admin_id : 0;
            }
        }
        return $admin_id;
    }
    public function getChannelQrTitle($user_id)
    {
        $dis = ["a.user_id" => $user_id, "a.is_new" => 1, "a.type" => 1];
        $title = $this->alias("a")->join("massage_channel_qr b", "a.qr_id = b.id")->where($dis)->group("a.id")->order("a.id desc")->value("b.title");
        return $title;
    }
    public function getQrTitle($user_id)
    {
        $dis = ["user_id" => $user_id, "is_new" => 1];
        $data = $this->where($dis)->field("type,qr_id")->order("id desc")->find();
        $arr = [["type" => 1, "table" => "massage_channel_qr", "title" => "title"], ["type" => 2, "table" => "massage_service_user_list", "title" => "nickName"], ["type" => 3, "table" => "massage_coach_broker_list", "title" => "user_name", "filed" => "user_id"], ["type" => 4, "table" => "shequshop_school_admin", "title" => "agent_name"], ["type" => 5, "table" => "shequshop_school_admin", "title" => "agent_name"], ["type" => 6, "table" => "shequshop_school_admin", "title" => "agent_name"], ["type" => 7, "table" => "massage_service_coach_list", "title" => "coach_name"], ["type" => 8, "table" => "massage_salesman_list", "title" => "user_name"], ["type" => 9, "table" => "massage_channel_list", "title" => "user_name"], ["type" => 10, "table" => "massage_distribution_list", "title" => "user_name"], ["type" => 11, "table" => "shequshop_school_admin", "title" => "agent_name"], ["type" => 12, "table" => "shequshop_school_admin", "title" => "agent_name"]];
        if(!empty($data)) {
            foreach ($arr as $v) {
                if($v["type"] == $data["type"]) {
                    $filed = isset($v["filed"]) ? $v["filed"] : "id";
                    $dis = [$filed => $data["qr_id"]];
                    if(isset($v["filed"])) {
                        $dis["status"] = 2;
                    }
                    $title = \think\facade\Db::name($v["table"])->where($dis)->value($v["title"]);
                    if($v["type"] == 1) {
                        $channel_id = \think\facade\Db::name($v["table"])->where([$filed => $data["qr_id"]])->value("channel_id");
                        $channel_model = new ChannelList();
                        $channel_title = $channel_model->where(["id" => $channel_id])->value("user_name");
                        if(!empty($channel_title)) {
                            $title = $channel_title . "-" . $title;
                        }
                    }
                    return $title;
                }
            }
        }
        return "";
    }
}

?>